package com.ruoyi.system.service.impl;

import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.ByBsMapper;
import com.ruoyi.system.mapper.ByJzMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ByWxUserMapper;
import com.ruoyi.system.domain.ByWxUser;
import com.ruoyi.system.service.IByWxUserService;

/**
 * 用户Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-12
 */
@Service
public class ByWxUserServiceImpl implements IByWxUserService
{
    @Autowired
    private ByWxUserMapper byWxUserMapper;

    @Autowired
    private ByBsMapper bsMapper;

    @Autowired
    private ByJzMapper jzMapper;


    @Override
    public ByWxUser queryByOid(String openid) {
        return byWxUserMapper.queryByOid(openid);
    }

    /**
     * 查询用户
     *
     * @param id 用户主键
     * @return 用户
     */
    @Override
    public ByWxUser selectByWxUserById(String id){
        ByWxUser entity = byWxUserMapper.selectByWxUserById(id);
        JSONObject json = bsMapper.getBsByWxid(id);
        if(json != null){
            if(!StringUtils.isEmpty(json.getString("name"))){
                entity.setNickname(json.getString("name"));
            }
        }
        JSONObject jsonJz = jzMapper.getDetailBywxid(id);
        if(jsonJz != null){
            if(!StringUtils.isEmpty(jsonJz.getString("name"))){
                entity.setNickname(jsonJz.getString("name"));
            }
        }
        return entity;
    }

    /**
     * 查询用户列表
     *
     * @param byWxUser 用户
     * @return 用户
     */
    @Override
    public List<ByWxUser> selectByWxUserList(ByWxUser byWxUser)
    {
        return byWxUserMapper.selectByWxUserList(byWxUser);
    }

    /**
     * 新增用户
     *
     * @param byWxUser 用户
     * @return 结果
     */
    @Override
    public int insertByWxUser(ByWxUser byWxUser)
    {
        return byWxUserMapper.insertByWxUser(byWxUser);
    }

    /**
     * 修改用户
     *
     * @param byWxUser 用户
     * @return 结果
     */
    @Override
    public int updateByWxUser(ByWxUser byWxUser)
    {
        byWxUser.setUpdateTime(DateUtils.getNowDate());
        return byWxUserMapper.updateByWxUser(byWxUser);
    }

    /**
     * 批量删除用户
     *
     * @param ids 需要删除的用户主键
     * @return 结果
     */
    @Override
    public int deleteByWxUserByIds(String[] ids)
    {
        return byWxUserMapper.deleteByWxUserByIds(ids);
    }

    /**
     * 删除用户信息
     *
     * @param id 用户主键
     * @return 结果
     */
    @Override
    public int deleteByWxUserById(String id)
    {
        return byWxUserMapper.deleteByWxUserById(id);
    }
}
